package cn.com.sise.dao.impl;

import cn.com.sise.dao.ClassDao;
import cn.com.sise.pojo.Class;
import cn.com.sise.pojo.Dept;
import cn.com.sise.pojo.Major;
import cn.com.sise.pojo.Teacher;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

//注解
@Repository("classDao")
public class ClassDaoImpl implements ClassDao
{
    @Resource
    private JdbcTemplate jdbcTemplate;


    @Override
    public int addClass(Class c) {
        //定义SQL语句
        String sql="insert into tb_class(class_id,class_name,class_size,class_surplus,dept_id,major_id,tutor_id,instructor_id) values(?,?,?,?,?,?,?,?)";
        //存放SQL语句的参数
        Object[] params=new Object[]{c.getClassId(),c.getClassName(),c.getClassSize(),c.getClassSurplus(),c.getDeptId(),c.getMajorId(),c.getTutorId(),c.getInstructorId()};
        //执行SQL，获取返回结果
        int rows=jdbcTemplate.update(sql,params);
        return rows;
    }

    @Override
    public int updateClass(Class c) {
        //定义SQL语句
        String sql="update tb_class set class_name=?,class_size=?,class_surplus=?,tutor_id=?,instructor_id=? where class_id=?";
        //存放SQL语句的参数
        Object[] params=new Object[]{c.getClassName(),c.getClassSize(),c.getClassSurplus(),c.getTutorId(),c.getInstructorId(),c.getClassId()};
        //执行SQL，获取返回结果
        int rows=jdbcTemplate.update(sql,params);
        return rows;
    }

    @Override
    public int deleteClassById(Integer classId) {
        //定义SQL语句
        String sql="delete from tb_class where class_id=?";
        //执行SQL，获取返回结果
        int rows=jdbcTemplate.update(sql,classId);
        return rows;
    }

    @Override
    public List<Class> findAllClasses() {
        String sql="select * from tb_class";
        RowMapper<Class> rowMapper= BeanPropertyRowMapper.newInstance(Class.class);
        List<Class> classes=jdbcTemplate.query(sql,rowMapper);
//        try
//        {
//            for (Class c : classes) {
//                if (c.getDeptId() != null) {
//                    System.out.println("c.getDeptId() !=null");
//                    RowMapper<Dept> Drm = BeanPropertyRowMapper.newInstance(Dept.class);
//                    sql="select * from tb_dept where  dept_id = ? ";
//                    System.out.println("sql:"+sql);
//                    c.setDept(jdbcTemplate.queryForObject(sql,Drm,c.getDeptId()));
//                    System.out.println("c.setDept");
//                }
//
//                if (c.getMajorId() != null && !c.getMajorId().equals("")) {
//                    System.out.println("c.getMajorId()!=null && ! c.getMajorId().equals(\"\")");
//                    RowMapper<Major> Mrm = BeanPropertyRowMapper.newInstance(Major.class);
//                    sql="select * from tb_major where  major_id = ? ";
//                    System.out.println("sql:"+sql);
//                    c.setMajor(jdbcTemplate.queryForObject(sql, Mrm,c.getMajorId()));
//                    System.out.println("c.setMajor");
//                }
//
//                if (c.getTutorId() != null && !c.getTutorId().equals("")) {
//                    System.out.println("c.getTutorId()!=null && ! c.getTutorId().equals(\"\")");
//                    RowMapper<Teacher> Trm = BeanPropertyRowMapper.newInstance(Teacher.class);
//                    sql="select * from tb_teacher where job_id = ? ";
//                    System.out.println("sql:"+sql);
//                    c.setTutor(jdbcTemplate.queryForObject(sql,Trm,c.getTutorId()));
//                    System.out.println("c.setTutor");
//                }
//
//                if (c.getInstructorId() != null && !c.getInstructorId().equals("")) {
//                    System.out.println("c.getInstructorId()!=null && ! c.getInstructorId().equals(\"\")");
//                    RowMapper<Teacher> Irm = BeanPropertyRowMapper.newInstance(Teacher.class);
//                    sql="select * from tb_teacher where job_id = ? ";
//                    System.out.println("sql:"+sql);
//                    c.setInstructor(jdbcTemplate.queryForObject(sql,Irm,c.getInstructorId()));
//                    System.out.println("c.setInstructor");
//                }
//                System.out.println("————");
//            };//for
//        }
//        catch(Exception e)
//        {
//            System.out.println("循环获取子Class时报错：");
//            System.out.println(Arrays.toString(e.getStackTrace()));
//        }

        return classes;
    }

    @Override
    public List<Class> findClassByName(String Classame) {
        //定义SQL语句
        String sql="select * from tb_class where class_name like concat('%',?,'%')";
        RowMapper<Class> rowMapper= BeanPropertyRowMapper.newInstance(Class.class);
        List<Class> classes=jdbcTemplate.query(sql,rowMapper,Classame);

        return classes;
    }

    @Override
    public List<Class> findAllClassByDept(Integer deptId) {
        String sql="select * from tb_class where dept_id=?";
        RowMapper<Class> rowMapper= BeanPropertyRowMapper.newInstance(Class.class);
        List<Class> classes=jdbcTemplate.query(sql,rowMapper,deptId);

        return classes;
    }

    @Override
    public List<Class> findAllClassByMajor(Integer majorId) {
        String sql="select * from tb_class where major_id=?";
        RowMapper<Class> rowMapper= BeanPropertyRowMapper.newInstance(Class.class);
        List<Class> classes=jdbcTemplate.query(sql,rowMapper,majorId);

        return classes;
    }

    @Override
    public int deleteClassesByIds(String[] id) {
        String  sql="delete from tb_class  where class_id in (";
        for (int i=0;i<id.length-1;i++){sql=sql+id[i]+",";}
        sql+=id[id.length-1]+")";
        System.out.println(sql);
        int rows=jdbcTemplate.update(sql);
        return rows;
    }

    @Override
    public Class findClassByClassId(int classId)
    {
        String sql="select * from tb_class where class_id=?";
        RowMapper<Class> rowMapper= BeanPropertyRowMapper.newInstance(Class.class);
        Class c=jdbcTemplate.queryForObject(sql,rowMapper,classId);
        return c;
    }
}
